//
//  SetSafetyCodeViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/2.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "SetSafetyCodeViewController.h"
#import "MQVerCodeInputView.h"
static NSInteger const kCodeLength = 6;
@interface SetSafetyCodeViewController ()
@property (nonatomic, weak)  MQVerCodeInputView *verView;
@property (nonatomic, strong) UILabel *tipLab;
@property (nonatomic, weak) UILabel *tipTitleLab;
@property (nonatomic, strong) NSString *fisrtCode;
@property (nonatomic, strong) NSString *secondCode;
@property (nonatomic, assign) SafetyCodeType safetyType;
@property (nonatomic, assign) BOOL validationPass;
@property (nonatomic, copy) SafetyCodeCallback callback;
@property (nonatomic, assign) BOOL noDismiss;
@end

@implementation SetSafetyCodeViewController
- (instancetype)initWithSafetyCodeType:(SafetyCodeType)safetyType{
    if (self == [super init]) {
        _safetyType = safetyType;
    }
    return self;
}
+ (instancetype)validationSafetyType:(SafetyCodeType)safetyType codeCallback:(SafetyCodeCallback)callback noDismiss:(BOOL)noDismiss{
    SetSafetyCodeViewController *vc = [[self alloc]initWithSafetyCodeType:safetyType];
    vc.callback = callback;
    vc.noDismiss = noDismiss;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.verView beginEdit];
}
- (void)backBtnClicked{
    if (self.DismissBlock) {
        self.DismissBlock();
    }
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:NO];
    }
}
- (void)setupUI{
    if ((self.safetyType == SafetyCodeTypeValidation || self.safetyType == SafetyCodeTypeRemove) &&  self.noDismiss) {
        self.isShowLeftBack = NO;
    }
    [self addNavigationItemWithImageNames:@[@"black_nav_back"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
    self.title =(self.safetyType == SafetyCodeTypeValidation || self.safetyType == SafetyCodeTypeRemove)?XLocalized(@"验证密码"):XLocalized(@"安全设置");
    UILabel *tipTitleLab = [UILabel new];
    _tipTitleLab = tipTitleLab;
    tipTitleLab.textColor = UIColorHex(#333333);
    tipTitleLab.font = kDefaultFont(16);
    tipTitleLab.text =(self.safetyType == SafetyCodeTypeValidation || self.safetyType == SafetyCodeTypeRemove)?XLocalized(@"请输入密码") : self.safetyType == SafetyCodeTypeSet?XLocalized(@"请输入应用密码"):XLocalized(@"请输入旧密码");
    [self.contentView addSubview:tipTitleLab];
    [tipTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(80));
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(15);
    }];
    MQVerCodeInputView *verView = [[MQVerCodeInputView alloc]initWithFrame:CGRectMake(AdaptedWidth(30), AdaptedWidth(140), kScreenWidth-AdaptedWidth(60), AdaptedWidth(35))];
    _verView = verView;
    verView.viewColorHL = kColorMain;
    verView.maxLenght = kCodeLength;//最大长度
    verView.keyBoardType = UIKeyboardTypeNumberPad;
    [verView mq_verCodeViewWithMaxLenght];
    kWeakSelf(self);
    verView.block = ^(NSString *text) {
        [weakself hideTip:YES];
        if (text.length==kCodeLength) {
            if (weakself.safetyType == SafetyCodeTypeSet) {
                if (!weakself.fisrtCode.length) {
                    weakself.fisrtCode = text;
                    [weakself.verView clear];
                    weakself.tipTitleLab.text = XLocalized(@"请确认您的新密码");
                    [weakself hideTip:NO];
                    [weakself.verView beginEdit];
                }else{
                    weakself.secondCode = text;
                    if (![weakself.secondCode isEqualToString:weakself.fisrtCode]) {
                        weakself.tipLab.text = XLocalized(@"密码不一致");
                        [weakself hideTip:NO];
                    }else{
                        [weakself hideTip:YES];
                        [kAppWindow makeToast:XLocalized(@"设置成功!")];
                        [weakself backBtnClicked];
                        SET_KLOCAl_Application_Key(weakself.secondCode);
                    }
                }
            }else if(weakself.safetyType == SafetyCodeTypeChange){
                if (!weakself.validationPass) {
                    if ([text isEqualToString:KLOCAl_Application_Key]) {
                        weakself.tipTitleLab.text = XLocalized(@"输入新应用密码");
                        [weakself.verView clear];
                        [weakself hideTip:YES];
                        [weakself.verView beginEdit];
                        weakself.validationPass = YES;
                    }else{
                        weakself.tipLab.text = XLocalized(@"密码错误!");
                        [weakself hideTip:NO];
                    }
                }else{
                    if (!weakself.fisrtCode.length){
                        weakself.fisrtCode = text;
                        [weakself.verView clear];
                        weakself.tipTitleLab.text = XLocalized(@"请确认您的新密码");
                        [weakself hideTip:YES];
                        [weakself.verView beginEdit];
                    }else{
                        weakself.secondCode = text;
                        if (![weakself.secondCode isEqualToString:weakself.fisrtCode]) {
                            weakself.tipLab.text = XLocalized(@"密码不一致!");
                            [weakself hideTip:NO];
                        }else{
                            [weakself hideTip:YES];
                            [kAppWindow makeToast:XLocalized(@"设置成功!")];
                            [weakself backBtnClicked];
                            SET_KLOCAl_Application_Key(weakself.secondCode);
                        }
                    }
                }
            }else{
                if ([text isEqualToString:KLOCAl_Application_Key]) {
                    if (weakself.safetyType == SafetyCodeTypeRemove) {
                        REMOVE_KLOCAl_Application_Key;
                    }
                    !weakself.callback?:weakself.callback(weakself,YES);
                }else{
                    weakself.tipLab.text = XLocalized(@"密码错误!");
                    [weakself hideTip:NO];
                    [weakself shakeTip];
                }
            }
        }
    };
    [self.contentView addSubview:verView];
    [self.contentView addSubview:self.tipLab];
    [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(verView.mas_bottom).offset(AdaptedWidth(50));
        make.bottom.mas_equalTo(AdaptedWidth(-100));
    }];
}
- (void)hideTip:(BOOL)hide{
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.tipLab.alpha = hide?0:1;
    } completion:^(BOOL finished) {
        
    }];
    if (!hide) {
        [self shakeTip];
    }
}
- (void)shakeTip
{
    POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    positionAnimation.velocity = @200;
    positionAnimation.springBounciness = 20;
    [positionAnimation setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
    }];
    [self.tipLab.layer pop_addAnimation:positionAnimation forKey:@"positionAnimation"];
}

- (UILabel*)tipLab{
    if (!_tipLab) {
        _tipLab = [UILabel new];
        _tipLab.font = kDefaultFont(18);
        _tipLab.textColor = RGBA(231, 76, 60, 1);
        _tipLab.alpha = 0;
    }
    return _tipLab;
}

@end
